<template>
  <div style="padding: 10px">
    <!-- 按钮区域 -->

    <div style="margin: 10px 0">
      <el-button type="primary" @click="ToaddMovie">新增</el-button>
    </div>
    <h3>大片管理</h3>
    <!-- 搜索框区域 -->

    <div style="margin: 10px 0">
      <el-input
        v-model="searchMovie"
        placeholder="请输入电影名"
        clearable
        style="width: 20%"
      />
      <el-button type="primary" style="margin-left: 10px" @click="findAll"
        >查询</el-button
      >
    </div>

    <!-- 表单区域开始 -->
    <el-table :data="tableData" border stripe style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        sortable
        fixed
      ></el-table-column>
      <el-table-column prop="name" label="电影名" ></el-table-column>
      <el-table-column prop="actor" label="主演"></el-table-column>
      <el-table-column
        prop="year"
        label="上映年份"

      ></el-table-column>

      <el-table-column prop="area" label="地区" sortable width="70">
        <template #default="scope">
          <span>{{
            scope.row.area == 0 ? "内地" : scope.row.area == 1 ? "港台" : "欧美"
          }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="url" label="地址" width="300"></el-table-column> -->
      <el-table-column
        prop="level"
        sortable
        label="评分"
        width="70"
      ></el-table-column>
      <el-table-column label="封面" width="100">
        <template #default="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.img"
            :preview-src-list="[scope.row.img]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template #default="scope">
          <!-- 加上@confirm 只有在确认被点击时才执行 -->
          <el-popconfirm
            title="确认删除吗?"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 表单区域结束 -->

    <!-- 分页开始 -->
    <div style="margin: 10px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        v-model:currentPage="pageNum"
        :page-sizes="[5, 10, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
    <!-- 分页结束 -->
  </div>
</template>

<script>
import request from "../../utils/request";

export default {
  name: "bigFilm",
  components: {},
  data() {
    return {
      searchMovie: "", //定义输入框
      currentPage: 1, //默认是1r
      pageSize: 10, //默认为10
      pageNum: 1,
      total: 0,
      dialogVisible: false, //默认关闭 (添加电影弹出框)
      dialogVisibleType: false,
      form: {}, //定义form对象为空
      tableData: [],
      sex: 0,
      drawer: false, //默认不显示抽屉
      typeId: 1,
    };
  },
  //页面加载时
  created() {
    this.findAll();
  },

  //方法写在data外面
  methods: {
    //查询所有电影
    findAll() {
      console.log(this.searchMovie);
      request
        .get("/type/findTypeMovie", {
          params: {
            //传入变量
            pageSize: this.pageSize,
            pageNum: this.pageNum,
            name: this.searchMovie,
            typeId: this.typeId,
          },
        })
        .then((res) => {
          console.log(res);
          //重点 将查询到的数据records 返回给表单中的tableData
          (this.tableData = res.data.list), (this.total = res.data.total);
        });
    },

    //删除电影
    handleDelete(id) {
      console.log(id);

      request.put("/type/deleteTypeMovie/" + id).then((res) => {
        if (res.code === "0") {
          this.$message({
            type: "success",
            message: "删除成功",
          });
        } else {
          this.$message({
            type: "error",
            message: res.message,
          });
        }
        this.findAll(); //重新加载删除之后表格的数据
      });
    },

    //改变条数
    handleSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.findAll();
    },

    //改变当前页码
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum;
      this.findAll();
    },
  },
};
</script>
